package com.colbby.datahouse.database;

import java.sql.Connection;
import java.util.Objects;

import com.colbby.datahouse.db.DBAccesser;
import com.colbby.datahouse.entity.Trade;
import com.colbby.datahouse.lock.Lock;
import com.colbby.datahouse.lock.LockFactory;
import com.colbby.datahouse.message.ConnectionUtils;

public class TradeInserter extends AbstractInserter<Trade> {
	
	private static final Lock<Long> lock = LockFactory.fac();

	@Override
	protected Trade findFromDatabase(Trade entity) {
		Connection conn = ConnectionUtils.getConnection();
		DBAccesser db = new DBAccesser(conn);
		Trade condition = new Trade();
		condition.setTid(entity.getTid());
		return db.selectOne(Trade.class, condition);
	}

	@Override
	protected void insertInto(Trade entity) {
		Connection conn = ConnectionUtils.getConnection();
		DBAccesser db = new DBAccesser(conn);
		db.save(entity);
	}

	@Override
	protected void update(Trade newOne, Trade oldOne) {
		if(newOne.getModified().getTime() <= oldOne.getModified().getTime()) return;
		Trade updated = new Trade();
		updated.setModified(newOne.getModified());
		if(!Objects.equals(newOne.getAdjustFee(), oldOne.getAdjustFee())) {
			updated.setAdjustFee(newOne.getAdjustFee());
		}
		if(!Objects.equals(newOne.getAlipayNo(), oldOne.getAlipayNo())) {
			updated.setAlipayNo(newOne.getAlipayNo());
		}
		if(!Objects.equals(newOne.getBuyerNick(), oldOne.getBuyerNick())) {
			updated.setBuyerNick(newOne.getBuyerNick());
		}
		if(!Objects.equals(newOne.getCodFee(), oldOne.getCodFee())) {
			updated.setCodFee(newOne.getCodFee());
		}
		if(!Objects.equals(newOne.getCommissionFee(), oldOne.getCommissionFee())) {
			updated.setCommissionFee(newOne.getCommissionFee());
		}
		if(!Objects.equals(newOne.getConsignTime(), oldOne.getConsignTime())) {
			updated.setConsignTime(newOne.getConsignTime());
		}
		if(!Objects.equals(newOne.getEndTime(), oldOne.getEndTime())) {
			updated.setEndTime(newOne.getEndTime());
		}
		if(!Objects.equals(newOne.getPayment(), oldOne.getPayment())) {
			updated.setPayment(newOne.getPayment());
		}
		if(!Objects.equals(newOne.getPayTime(), oldOne.getPayTime())) {
			updated.setPayTime(newOne.getPayTime());
		}
		if(!Objects.equals(newOne.getPostFee(), oldOne.getPostFee())) {
			updated.setPostFee(newOne.getPostFee());
		}
		if(!Objects.equals(newOne.getPrice(), oldOne.getPrice())) {
			updated.setPrice(newOne.getPrice());
		}
		if(!Objects.equals(newOne.getRealPointFee(), oldOne.getRealPointFee())) {
			updated.setRealPointFee(newOne.getRealPointFee());
		}
		if(!Objects.equals(newOne.getReceivedPayment(), oldOne.getReceivedPayment())) {
			updated.setReceivedPayment(newOne.getReceivedPayment());
		}
		if(!Objects.equals(newOne.getReceiverAddress(), oldOne.getReceiverAddress())) {
			updated.setReceiverAddress(newOne.getReceiverAddress());
		}
		if(!Objects.equals(newOne.getReceiverCity(), oldOne.getReceiverCity())) {
			updated.setReceiverCity(newOne.getReceiverCity());
		}
		if(!Objects.equals(newOne.getReceiverDistrict(), oldOne.getReceiverDistrict())) {
			updated.setReceiverDistrict(newOne.getReceiverDistrict());
		}
		if(!Objects.equals(newOne.getReceiverMobile(), oldOne.getReceiverMobile())) {
			updated.setReceiverMobile(newOne.getReceiverMobile());
		}
		if(!Objects.equals(newOne.getReceiverName(), oldOne.getReceiverName())) {
			updated.setReceiverName(newOne.getReceiverName());
		}
		if(!Objects.equals(newOne.getReceiverPhone(), oldOne.getReceiverPhone())) {
			updated.setReceiverPhone(newOne.getReceiverPhone());
		}
		if(!Objects.equals(newOne.getReceiverState(), oldOne.getReceiverState())) {
			updated.setReceiverState(newOne.getReceiverZip());
		}
		if(!Objects.equals(newOne.getReceiverZip(), oldOne.getReceiverZip())) {
			updated.setReceiverZip(newOne.getReceiverZip());
		}
		Connection conn = ConnectionUtils.getConnection();
		DBAccesser db = new DBAccesser(conn);
		Trade condition = new Trade();
		condition.setTid(oldOne.getTid());
		db.update(updated, condition);
	}

	@Override
	protected void lock(Trade entity) {
		lock.lock(entity.getTid());
	}

	@Override
	protected void unlock(Trade entity) {
		lock.unlock(entity.getTid());
	}
}